/*
 * con_stuff.c
 *
 * Created: 2012-08-22 20:40:26
 *  Author: Antrykot
 */ 

#include "../sc-s/main.h"
#include "../sc-s/lcd.h"
#include "../sc-s/sc-s.h"
#include <stdlib.h>
#include "../sc-s/trans-s.h"

#define INTRO_DELAY	1000

#if defined(MEDIA_CON) | defined(MEDIA_COS)
void display_name()
{
#ifdef MEDIA_CON
	lcd_print("SC-CO200");
#elif MEDIA_COS
	lcd_print("SC-CO/S ");
#endif
}
#endif

void display_alarms()
{
	char str[8];
	lcd_home();
	lcd_print("AL1:");
	itoa(AL1,str,10);
	lcd_print(str);
	lcd_print("   ");
	WAITMS(INTRO_DELAY);

	lcd_home();
	lcd_print("AL2:");
	itoa(AL2,str,10);
	lcd_print(str);
	lcd_print("   ");
	WAITMS(INTRO_DELAY);
}

void calc_dac_out()

{
	if (!uout)
	{
		napb = 0;
		napa = 0;
		return;
	}
	if (uout == 2)	// in calib;
	return;
	
	u16 tmp = meas;
	float fnap;
	
	
	if (tmp & 0x4000)	// 14bit
	{
		tmp &= 0x3FFF;
		
		if (tmp & 0x1000)		// 12bit  - minus
		{
			tmp &= 0x0FFF;
			
			if (tmp < 10)		// -10ppm
			{
				napb = U2_40;
				napa = U1_40;
				return;
			}
			napa = U1_35;
			napb = U2_35;
			return;
		}
		
#ifdef MEDIA_CON		
		
		if (tmp > 200)
		{
			napa = U1_21;
			return;
		}
		
		fnap = ( U1_20 - U1_40 ) / (float)200;
		fnap = fnap * tmp ;
		napa = fnap + U1_40;
#elif MEDIA_COS
		if (tmp > 999)
		{
			napa = U1_21;
			return;
		}
		
		fnap = ( U1_20 - U1_40 ) / (float)1000;
		fnap = fnap * tmp ;
		napa = fnap + U1_40;
#endif
	}
	
	
}

void config_menu()
{
	char str[8];
	
	switch(calib_ptr_deep)
	{
		case 0x00:
		{
			lcd_home();
			lcd_print(" KONFIG ");
			WAITMS(1000);
			lcd_home();
			lcd_print("SET[-/+]");
			WAITMS(1000);
			calib_ptr_deep++;
			break;
		}
		case 0x01:
		{
			lcd_home();
			switch(Ksp)
			{
				case 0:
				lcd_print(" BRAK   ");
				break;
				case 1:
				lcd_print(" KSP-2  ");
				break;
				case 2:
				lcd_print(" KSP-3  ");
				break;
				
			}
			if (!PLU)
			{
				Ksp++;
			}
			
			if (!MIN)
			{
				Ksp--;
			}
			
			if (Ksp > 2)
			Ksp = 0;

			if (!ENT)
			{
				_write(EEP_KSP,Ksp);
				
				if (Ksp == 0 || Ksp == 1)
				{
					tk_data = 0;		// analog
					tk_addr = K_ADDR_KSP;
				}
				else
				{
					tk_data = 1;		// digtrans
					tk_addr = K_ADDR_KSP;
				}
				
				calib_ptr_deep++;
			}
			break;
		}
		case 0x02:
		{
			lcd_home();
			lcd_print("AWP ");
			if (Awp)
			{
				lcd_print("ZAL ");
			}
			else
			{
				lcd_print("WYL ");
			}
			if (!PLU)
			{
				Awp = 1;
			}
			if (!MIN)
			{
				Awp = 0;
			}
			if (!ENT)
			{
				_write(EEP_AWP,Awp);
				if (Awp)
				{
					calib_ptr_deep += 1;
				}
				else
				{
					calib_ptr_deep = 0x05;	// kcl
				}
			}
			break;
		}
		case 0x03:
		{
			lcd_home();
			lcd_print("AL1=");
			itoa(AL1,str,10);
			lcd_print(str);
			lcd_print("  ");
			
			if (!PLU)
			AL1++;
			if (!MIN)
			AL1--;
			
			if (AL1 < 4)
			AL1 = 4;
			if (AL1 > AL2)
			AL1 = AL2;
			
			if (!ENT)
			{
				calib_ptr_deep++;
				write_word(EEP_AL1,AL1);
			}
			break;
		}
		case 0x04:
		{
			lcd_home();
			lcd_print("AL2=");
			itoa(AL2,str,10);
			lcd_print(str);
			lcd_print("  ");
			
			if (!PLU)
			AL2++;
			if (!MIN)
			AL2--;
			
			if (AL2 < AL1)
			AL2 = AL1;
			if (AL2 > 26)
			AL2 = 26;
			
			if (!ENT)
			{
				calib_ptr_deep++;
				write_word(EEP_AL2,AL2);
			}
			break;
		}
		case 0x05:
		{
			lcd_home();
			lcd_print("KCL ");
			if (Kcl)
			{
				lcd_print("ZAL ");
			}
			else
			{
				lcd_print("WYL ");
			}
			if (!PLU)
			{
				Kcl = 1;
			}
			if (!MIN)
			{
				Kcl = 0;
			}
			if (!ENT)
			{
				_write(EEP_KCL,Kcl);
				calib_ptr_deep += 1;

			}
			break;
		}
		case 0x06:
		{
			lcd_home();
			lcd_print("WY-U ");
			if (uout)
			{
				lcd_print("ZAL ");
			}
			else
			{
				lcd_print("WYL ");
			}
			if (!PLU)
			{
				uout = 1;
			}
			if (!MIN)
			{
				uout = 0;
			}
			if (!ENT)
			{
				_write(EEP_UOUT,uout);
				calib_ptr_deep += 1;

			}
			break;
		}
		case 0x07:
		{
			lcd_home();
			lcd_print("LCD ");
			if (lcdmode)
			{
				lcd_print("ZAL ");
			}
			else
			{
				lcd_print("TIME");
			}
			if (!PLU)
			{
				lcdmode = 1;
			}
			if (!MIN)
			{
				lcdmode = 0;
			}
			if (!ENT)
			{
				_write(EEP_LCDM,lcdmode);
				calib_ptr_deep += 1;

			}
			break;
		}
		case 0x08:
		{
			lcd_home();
			lcd_print("ZAPISANE");
			WAITMS(2000);
			Pro = PRO_WORK;
			intro();
			break;
		}
	}
	
	if (!PLU || !MIN)
	{
		WAITMS(100);
	}
	
	while( !ENT);
	
}


